package tpi.gui.pages.villano

import dominio.individuos.Sexo
import dominio.individuos.Villano
import org.apache.wicket.markup.html.WebPage
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.commons.model.UserException
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import tpi.gui.panels.info.EditarHobbiesPanel
import tpi.gui.panels.info.EditarRasgosPanel
import tpi.gui.panels.info.VerHobbiesPanel
import tpi.gui.panels.info.VerRasgosPanel
import tpi.gui.pages.expedientes.ExpedientesPage
import org.apache.wicket.markup.html.form.DropDownChoice
import org.apache.wicket.model.PropertyModel

abstract class VillanoPage extends WebPage {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Villano original
	private final Villano copia
	private Sexo sexoSeleccionado
	private final Boolean conEdicion
	private final ExpedientesPage mainPage
	
	new(Villano original,Villano copia, Boolean conEdicion, ExpedientesPage mainPage) {
		this.defaultModel = copia.asCompoundModel
		this.original = original
		this.copia = copia
		this.sexoSeleccionado = copia.sexo
		this.conEdicion = conEdicion
		this.mainPage = mainPage
		
		if (conEdicion) addChild(new Label("titulo", if(editando) "Edicion de Villano" else "Nuevo Villano"))
		else addChild(new Label("titulo", "Expedientes"))
		val Form<Villano> form = new Form<Villano>("villanoForm", new CompoundPropertyModel(this.copia))
		if (conEdicion) form.addChild(new TextField<String>("nombre"))
		else form.addChild(new Label("nombre"))
//		form.addChild(new DropDownChoice("sexo",
//             		  new PropertyModel(this.copia, "sexo"),
//             		  new PropertyModel(this.copia,"sexosPosibles")))
		form.agregarRasgosPanel
		form.agregarHobbiesPanel
		if(conEdicion) {
			form.addChild(new FeedbackPanel("feedbackPanel"))
			form.addChild(new XButton("aceptar").onClick = [| aceptar()])
		}
		form.addChild(new XButton("volver").onClick = [| volver()])

		addChild(form)
	}
	
	def agregarRasgosPanel(Form<Villano> mainForm) {
		if (conEdicion)
			mainForm.addChild(new EditarRasgosPanel("rasgos", this.copia))
		else
			mainForm.addChild(new VerRasgosPanel("rasgos", this.copia))
		
	}

	def agregarHobbiesPanel(Form<Villano> mainForm) {
		if (conEdicion) 
			mainForm.addChild(new EditarHobbiesPanel("hobbies", this.copia))
		else 
			mainForm.addChild(new VerHobbiesPanel("hobbies", this.copia))
	}
	
	def aceptar() {
		try {
			copia.validar()
			if(editando) {
				val index = original.expedientes.villanos.indexOf(original)
				copia.sexo = sexoSeleccionado
				original.expedientes.villanos.set(index, copia)
			} else {
				copia.sexo = sexoSeleccionado
				copia.expedientes.agregarVillano(copia)
			}
			responsePage = mainPage
		} catch (UserException e) {
			info(e.getMessage())
		} 
	}
	
	def editando() {
		original != null
	}
	
	def volver() {
		responsePage = this.mainPage
	}
	
}